package com.sskj.uboex.ui.activity;


import android.Manifest;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.PermissionTipUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CopyUtil;
import com.sskj.uboex.R;
import com.sskj.uboex.bean.RechargeInfoBean;
import com.sskj.uboex.presenter.RechargeActivityPresenter;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;

@Route(path = RConfig.APP_RECHARGE)//充币
public class RechargeActivity extends BaseActivity<RechargeActivityPresenter> {
    @BindView(R.id.ivQRCode)
    ImageView ivQRCode;
    @BindView(R.id.tvSave)
    TextView tvSave;
    @BindView(R.id.tvAddress)
    TextView tvAddress;
    @BindView(R.id.tvCopy)
    TextView tvCopy;
    @Autowired
    String code;
    @Autowired
    String type;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_recharge;
    }

    @Override
    public RechargeActivityPresenter getPresenter() {
        return new RechargeActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle("充币");

    }

    @Override
    protected void initData() {
        mPresenter.recharge(code, type);
    }

    public void updateView(RechargeInfoBean bean) {
        tvAddress.setText("充币地址：" + bean.getAddr());
        if (TextUtils.isEmpty(bean.getAddr())) {
            ToastUtil.showShort(App.INSTANCE.getString(com.sskj.mine.R.string.mineRechargeActivity4));
            return;
        }
        ImageUtil.setImage(bean.getQrc(), ivQRCode);
        ClickUtil.click(tvCopy, () -> {
            CopyUtil.copy(bean.getAddr());
        });
        ClickUtil.click(tvSave, () -> {
            new RxPermissions(RechargeActivity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(isGrant -> {
                        if (isGrant) {
                            ImgUtil.saveImageToGallery(this, ivQRCode);

                        } else {
                            PermissionTipUtil.tip(RechargeActivity.this, App.INSTANCE.getString(com.sskj.mine.R.string.mineRechargeActivity5));
                        }
                    });

        });

    }
}
